#include "./view.h"

/****SGUI 按键模板*************
占用资源：
obj->data[0] 按钮是否按下
obj->data[1] 按钮触发回调
obj->data[2] 按下时选中
obj->data[3] 长按回调
obj->data[4] 长按时长
obj->data[5] 记录时间

回调函数：
sBtnClick
按下，抬起共两次回调
sBtnClick
长按，共回调一次
******************************/

//创建按钮
sObj*sGuiNewBtn(int x,int y,sSpr*spr,sBtnClick click)
{
 sObj*obj = sNewObj(x,y,spr);
 obj->eve = sGuiBtnEve;
 obj->data[0] = FALSE;
 obj->data[1] = (int)click;
 obj->data[2] = FALSE;
 return obj;
}

//长按事件
void sGuiBtnLong(sObj*obj,sBtnClick click,int time)
{
 obj->data[3] = (int)click;
 obj->data[4] = time;
 obj->data[5] = getuptime();
}

//监控按钮
void sGuiBtnEve(void*ob,int type,int p1,int p2)
{
 sObj*obj = ob;
 sBtnClick click = (sBtnClick)
 obj->data[1];
 int tmp = -1;
 //检测有没有按到
 tmp = 
 sPosCollObj(p1,p2,obj);
 switch(type)
 {
  //手移动和按下
  case MS_DOWN:
  if(tmp)
  {
   obj->data[0] = TRUE;
   obj->data[2] = TRUE;
   //按钮被按下瞬间
   if(obj->data[1]!=-1)
   	(*click)(obj,MS_DOWN);
   obj->data[5] = getuptime();
  }
  break;
  
  //手指移动
  case MS_MOVE:
  if(tmp)
  {
   obj->data[0] = TRUE;
   tmp = getuptime()-obj->data[5];
   if(obj->data[2]==TRUE)
   if(obj->data[3]!=-1)
   	if(tmp>=obj->data[4])
   {
    //触发长按事件
    click = (sBtnClick)obj->data[3];
    (*click)(obj,MS_MOVE);
    obj->data[2] = FALSE;
   }
  }
  //手指脱离按钮范围
  else
  {
   obj->data[0] = FALSE;
   obj->data[2] = FALSE;
  }
  break;
  
  case MS_UP:
  obj->data[0] = FALSE;
  //按钮内抬起 真单击事件
  if(tmp)
  {
   if(obj->data[1]!=-1)
   	if(obj->data[2]==TRUE)
   {
    click = (sBtnClick)
    obj->data[1];
    (*click)(obj,MS_UP);
   }
  }
  break;
 }
}

//绘制V9按钮
//对象，坐标，真实大小，裁剪边缘
void sGuiDrawBtnV9(sObj*obj,int x,int y,int rw,int rh,int dw,int dh)
{
 //精灵刷新
 int dx,dy,w,h;
 dx = obj->spr->x;
 dy = obj->spr->y;
 w = obj->spr->w;
 h = obj->spr->h;
 sRefObjSpr(obj);
 
 //更新obj图片
 sSetActId(obj->spr,obj->act);
 sSetImgId(obj->spr,obj->img);
 
 //按照.9图绘制
 sDrawSprV9(obj->spr,
 x,y,rw,rh,dw,dh);
 
 //精灵恢复
 obj->spr->x = dx;
 obj->spr->y = dy;
 obj->spr->w = w;
 obj->spr->h = h;
}




